let Api = require('../../utils/api.js');
let wxRequest = require('../../utils/wxRequest.js');
let common = require('../../utils/common.js')
let util = require("../../utils/util.js")

Page({
  data: {
    hosp: '全部',
    hospId: 0,
    ishospshow: false,
    hospcolumns: [],
    date: '',
    time: '',
    token: '',
    dataRec: {},
    dataJs: [],
    total: 0,
    yj: 0,
    je: 0,
    glf: 0,
  },
  onLoad: function (options) {
    let self = this
    let user = wx.getStorageSync('user')
    if (user) {
      self.setData({
        token: user.code
      })
      setInterval(function () {
        self.setDate()
      }, 1000)
      self.initHosp()
    } else {
      wx.reLaunch({
        url: '../login/login',
      })
    }
  },
  initHosp: function () {
    common.initHosp(this)
  },
  setDate: function () {
    let date = new Date();
    this.setData({
      date: util.formatDate(date),
      time: this.formatTime(date)
    })
  },
  formatTime: function (date) {
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [hour, minute, second].map(util.formatNumber).join(':')
  },
  showhosptap: function () {
    this.setData({
      ishospshow: true
    })
  },
  onhospcancel: function () {
    this.setData({
      ishospshow: false
    })
  },
  onhospconfirm: function (e) {
    var value = e.detail.value
    this.setData({
      hosp: value.text,
      hospId: value.id,
      ishospshow: false
    })
  },
  formsubmit: function (e) {
    let lDate = new Date()
    let lMonth = lDate.getMonth() + 1
    let lSql = "SELECT Count(1) as num,B.iID as hosp,B.nTcje as tcje,IsNull(C.cJssj,'1') as jsr,IsNull(B.cJsMonth,'0') as jsy FROM BD_BedLock A,BD_Hosp B,BD_Agent C WHERE B.iID=A.iHosp AND C.iID = A.iAgent AND A.iAgent = " + this.data.token
    if (this.data.hospId != 0) {
      lSql += " and A.iHosp = " + this.data.hospId
    }
    lSql += ' group by B.iID,B.nTcje,C.cJssj,B.cJsMonth'
    wxRequest.getRequest(Api.dbSqlQuery(lSql), null).then(res => {
      if (res.data) {
        let dataJs = res.data
        if (dataJs.length > 0) {
          if (dataJs[0].jsr < lDate.getDate() || dataJs[0].jsy == lMonth) {
            this.getTotal(0)
          } else {
            let lGlf = 0
            for (let i = 0; i < dataJs.length; i++) {
              lGlf += (dataJs[i].num * dataJs[i].tcje)
            }
            this.getTotal(lGlf)
          }
        }
        self.setData({
          dataJs: dataJs
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '查询失败',
        })
      }
    }).finally(fina => {

    })
  },
  getTotal: function (aglf) {
    let self = this
    let hosp = self.data.hospId
    let paramurl = "&c=3&hosp=" + hosp + "&amt="
    let token = self.data.token
    wx.showLoading({
      title: '正在统计...',
    })
    wxRequest.getRequest(Api.agTx(token, paramurl), null).then(res => {
      if (res.data) {
        self.initTotal(res.data, aglf)
      } else {
        wx.showModal({
          title: '提示',
          content: '统计数据失败!'
        })
      }
    }).finally(fina => {
      wx.hideLoading()
    })
  },
  initTotal: function (data, aglf) {
    let self = this
    let rec = self.data.dataRec
    rec.je = 0.0
    rec.yj = 0.0
    rec.glf = 0.0
    if (data.length == 0) {
      rec.ftime = ''
      rec.ltime = ''
      rec.oid = ''
      if (aglf > 0) {
        rec.glf += aglf
      }
    } else {
      for (let i = 0; i < data.length; i++) {
        if (i == 0) {
          rec.ftime = data[i].ftime
          rec.ltime = data[i].ltime
          rec.oid = data[i].oid
        }
        rec.yj += data[i].yj
        rec.je += data[i].je
        if (data[i].tcbl > 0) {
          rec.glf += (data[i].je * data[i].tcbl)
        }
      }
      if (aglf > 0) {
        rec.glf += aglf
      }
    }
    self.setData({
      dataRec: rec,
      total: util.formatLen(rec.je + rec.yj, 2),
      yj: util.formatLen(rec.yj, 2),
      je: util.formatLen(rec.je, 2),
      glf: util.formatLen(rec.glf, 2)
    })
  },
  sqjstap: function () {
    let self = this
    wx.showModal({
      title: '提示',
      content: '是否申请结算?',
      success(res) {
        if (res.confirm) {
          self.js()
        }
      }
    })
  },
  js: function () {
    let self = this
    let rec = self.data.dataRec
    if (util.isEmpty(rec.oid)) {
      wx.showModal({
        title: '提示',
        content: '没有可结算订单!'
      })
      return
    }
    let paramurl = "c=1&oid=" + rec.oid + "&mny=" + rec.je + "&hosp=" + self.data.hospId +
      "&st=" + rec.ftime + "&et=" + rec.ltime + "&glf=" + rec.glf + "&hospname=" + self.data.hosp
    wx.showLoading({
      title: '正在申请...',
    })
    wxRequest.getRequest(Api.agTx(self.user.token, paramurl), null).then(res => {
      if (res.data) {
        if (res.data == 0) {
          self.updateJs()
        } else {
          wx.showModal({
            title: '提示',
            content: '结算失败!'
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: '申请结算失败!'
        })
      }
    }).finally(fina => {
      wx.hideLoading()
    })
  },
  updateJs: function () {
    let date = new Date()
    let dataJs = this.data.dataJs
    let lSql = "UPDATE BD_Hosp SET cJsMonth = '" + (date.getMonth() + 1) + "' WHERE iID in ("
    let lParm = "";
    let i, n = dataJs.length;
    for (i = 0; i < n; i++) {
      lParm += dataJs[i].hosp + ',';
    }
    lParm += '0';
    lSql = lSql + lParm + ")"
    wxRequest.getRequest(Api.dbSqlExec(lSql), null).then(res => {
      if (res.data) {
        wx.showModal({
          title: '提示',
          content: '结算成功!'
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '结算失败!'
        })
      }
    }).finally(fina => {

    })
  },
  jsmxtap: function () {
    wx.navigateTo({
      url: '../finance_detail/index',
    })
  }
})